package com.jason.fingerprint.ui;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.fpi.MtRfid;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.R;
import com.jason.fingerprint.adapter.PictureGridAdapter;
import com.jason.fingerprint.adapter.SignInRecordAdapter;
import com.jason.fingerprint.beans.RecordBean;
import com.jason.fingerprint.beans.SignInRecordBean;
import com.jason.fingerprint.beans.SponsorInfo;
import com.jason.fingerprint.beans.ui.FingerPrintBean;
import com.jason.fingerprint.beans.ui.ImageOrAudioBean;
import com.jason.fingerprint.beans.ui.SoundBean;
import com.jason.fingerprint.common.DataHelper;
import com.jason.fingerprint.common.DateFormat;
import com.jason.fingerprint.common.DialogHelper;
import com.jason.fingerprint.common.UIHelper;
import com.jason.fingerprint.configs.DataConfig;
import com.jason.fingerprint.logic.MatchFingerprintSyncLogic;
import com.jason.fingerprint.logic.SignInUploadLogic;
import com.jason.fingerprint.logic.UploadToTempSyncLogic;
import com.jason.fingerprint.utils.DataUtils;
import com.jason.fingerprint.widget.ExpandListView;
import com.jason.fingerprint.widget.LoadingDialog;
import org.kymjs.aframe.database.DbModel;
import org.kymjs.aframe.database.KJDB;
import org.kymjs.aframe.utils.StringUtils;

import java.util.*;

/**
 * @ClassName: SignInDetailActivity
 * @Description: 社区服务、教育学习详细类，支持各种签到、拍照、录音及上传功能
 * @Author: John zhuqiangping@gmail.com
 * @Date: 2014年11月12日
 *
 */
public class SignInDetailActivity extends Activity implements OnClickListener{
	private static final String TAG = "SignInDetailActivity";

	private static final boolean DEBUG = false;

	private static final int MSG_LOAD_DATA = 1; // 初始化后加载数据

	private static final long ANIMATION_DURATION = 200L;

	public static final int RESULT_OK = 1001;
	public static final int SPONSOR_CHOOSE_PHOTO_REQUEST = 1002;
	public static final int SIGNIN_CHOOSE_PHOTO_REQUEST = 1003;

	private static final long FINGER_AGAIN_TIME = 1500L;

	private AppContext mAppContext;
	private KJDB mKjdb;

	private SponsorInfo mSponsorInfo; // 当前社区服务或者教育学习

	private Button mBackBtn; // 标题栏中back按钮
	private TextView mTitleView; // 标题栏中标题名

	private TextView mSoundRecordName;
	private TextView mSignInDetailTime;
	private TextView mSignInDetailAddress;
	private TextView mSignInDetailContent;
	private View mBaseDetail;
	private View mOthersDetail;
	private View mContentDetail;
	private View mControlDetail;
	private ExpandListView mListView;
	private TextView mSigninNote;

	private Button mBeginSignIn; // 开始签到
	private Button mCallSignIn; // 点名签到
	private Button mEndSignIn; // 结束签到
	private Button mCheckRecord; // 查看签到记录
	private Button mSoundRecord; // 录音
	private Button mTakePicture; // 拍照
	private Button mUploadRecord; // 上传记录
	private GridView mGridPictures; // 照片集合图
	private String mFlag; // 签到类型 0开始签到，1点名签到，2结束签到
	private String mRegisterType = "1"; // 0-集中教育 1-集中劳动 2-个别教育 3-个别劳动
	private SignInRecordAdapter mAdapter;

	private List<SignInRecordBean> mRecordList;
	private PictureGridAdapter mPictureGridAdapter;
	//	private ProgressDialog mUploadDialog; // 上传记录对话框
	private int mUploadIndex = 0; // 上传序列
	private boolean mAnimationRunning = false; // 动画运行中
	private boolean mExpandShow = false; // 展开签到详细界面
	private float mOffsetY = 0; // Y轴动画偏移量

	private int mChooseImageCount = 3;
	private int mTakePictureItem = -1;

	private List<String> mCacheCheckImages = new ArrayList<String>();

	private String mLastFileName;

	private MatchFingerprintSyncLogic mLogic;
	private int mSponsorType = 0;// 签到方式 0指纹，1，平台录入，2打卡
	private LoadingDialog mProgressDialog;
	private String mTitleName;

	private RecordBean mRecordBean;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case MSG_LOAD_DATA:
					if (mSponsorInfo != null) {
						// 从数据库中加载录音信息，仅支持集中劳动和集中教育
						StringBuilder where = new StringBuilder();
						where.append("sponsorType = '");
						where.append(mRegisterType);
						where.append("' and sponsorId = '");
						where.append(mSponsorInfo.getSponsorId());
						where.append("'and fileType = 2");
						List<ImageOrAudioBean> list = new ArrayList<ImageOrAudioBean>();
						try {
							list = mKjdb.findAllByWhere(
									ImageOrAudioBean.class, where.toString());
						} catch (Exception e) {
						}
						if (list != null && !list.isEmpty()) {
							// 默认情况下只有一条数据，故使用第一条数据
							mLastFileName = list.get(0).getFileName();
							mSoundRecordName.setText(mLastFileName);
//						mSoundRecord.setEnabled(false);
						}
						// 从数据库中加载图片资源
						mPictureGridAdapter.clear(); // 清空数组
						StringBuilder where2 = new StringBuilder();
						where2.append("sponsorType = '");
						where2.append(mRegisterType);
						where2.append("' and sponsorId = '");
						where2.append(mSponsorInfo.getSponsorId());
						where2.append("'and fileType = 1");
						List<ImageOrAudioBean> list2 = new ArrayList<ImageOrAudioBean>();
						try {
							list2 = mKjdb.findAllByWhere(
									ImageOrAudioBean.class, where2.toString());
						} catch (Exception e) {
							// TODO: handle exception
						}
						if (list2 != null && !list2.isEmpty()) {
							for (ImageOrAudioBean bean : list2) {
								mPictureGridAdapter.addBitmap(bean.getFilePath());
							}
						}
					}

					// 从数据库中加载签到信息
					StringBuilder where = new StringBuilder();
					where.append("type = '");
					where.append(mRegisterType);
					where.append("'");
//				where.append(" and state = 1");
					if (mSponsorInfo != null) {
						where.append(" and sponsorId = '");
						where.append(mSponsorInfo.getSponsorId());
						where.append("'");
					}
					try {
						mRecordList = mKjdb.findAllByWhere(SignInRecordBean.class,
								where.toString());
						Collections.sort(mRecordList, mRecordComparator);
					} catch (Exception e) {
					}
					mAdapter.setSignInRecordList(mRecordList);
					mAdapter.notifyDataSetChanged();
					break;
			}
		}

	};

	final Comparator mRecordComparator = new Comparator<SignInRecordBean>() {

		@Override
		public int compare(SignInRecordBean arg0, SignInRecordBean arg1) {
			Date date0 = DataUtils.convertStrToDate(arg0.getTime(),
					DateFormat.DATE_YMD_HMS);
			Date date1 = DataUtils.convertStrToDate(arg1.getTime(),
					DateFormat.DATE_YMD_HMS);
			if (date0.before(date1)) {
				return 1;
			}
			return -1;
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mAppContext = (AppContext) getApplication();
		mKjdb = mAppContext.getKjdb();
		mProgressDialog = new LoadingDialog(this);
		setContentView(R.layout.activity_singindetail);
		initView();
		initFinger();
	}

	// 初始化指纹信息
	private void initFinger() {
		MtRfid.getInstance().RfidInit();
		MtRfid.getInstance().SetContext(this);

//		mLogic = MatchFingerprintSyncLogic.getInstance();
		mLogic = new MatchFingerprintSyncLogic();
//		mLogic.setFingerPrintReader(SerialPortManager.getInstance()
//				.getNewAsyncFingerprint());
		mLogic.setHandle(mFingerpringHandler);
		mLogic.isManager(false);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mHandler.sendEmptyMessage(MSG_LOAD_DATA);
	}

	// 初始化数据
	private void initView() {

		mBackBtn = (Button) this.findViewById(R.id.header_back);
		mBackBtn.setOnClickListener(this);

		mTitleView = (TextView) this.findViewById(R.id.header_title);
		mTitleView.setText(R.string.centralized_labor);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			if (extras.containsKey("SponsorInfo")) {
				mSponsorInfo = (SponsorInfo) extras.get("SponsorInfo");
			}
			if (extras.containsKey("title")) {
				mTitleName = extras.getString("title");
				mTitleView.setText(mTitleName);
			}

			mRegisterType = extras.getString("registerType",
					DataConfig.REGISTER_TYPE_1);
		}

		mBaseDetail = this.findViewById(R.id.singindetail_base);
		mSignInDetailTime = (TextView) mBaseDetail
				.findViewById(R.id.signin_detail_date);
		mSignInDetailAddress = (TextView) mBaseDetail
				.findViewById(R.id.signin_detail_address);
		mSignInDetailContent = (TextView) mBaseDetail
				.findViewById(R.id.signin_detail_content);

		mOthersDetail = this.findViewById(R.id.singindetail_others);
		mSoundRecordName = (TextView) mOthersDetail
				.findViewById(R.id.record_name);

		mContentDetail = this.findViewById(R.id.singindetail_content);
		mListView = (ExpandListView) this.findViewById(R.id.singindetail_list);
		mControlDetail = this.findViewById(R.id.singindetail_control);
		mSigninNote = (TextView) this.findViewById(R.id.signin_note);

		TextView emptyView = (TextView) this
				.findViewById(R.id.signin_detail_empty_pictures);
		mGridPictures = (GridView) this
				.findViewById(R.id.signin_detail_grid_pictures);
		mGridPictures.setEmptyView(emptyView);
		mPictureGridAdapter = new PictureGridAdapter(this);
		mGridPictures.setAdapter(mPictureGridAdapter);

		mBeginSignIn = (Button) this.findViewById(R.id.bengin_signin);
		mBeginSignIn.setOnClickListener(this);
		mCallSignIn = (Button) this.findViewById(R.id.call_signin);
		mCallSignIn.setOnClickListener(this);
		mEndSignIn = (Button) this.findViewById(R.id.end_signin);
		mEndSignIn.setOnClickListener(this);
		mCheckRecord = (Button) this.findViewById(R.id.check_record);
		mCheckRecord.setOnClickListener(this);
		mSoundRecord = (Button) this.findViewById(R.id.sound_record);
		mSoundRecord.setOnClickListener(this);
		// 屏蔽集中教育和集中签到中的录音模块
		mSoundRecord.setVisibility(View.GONE);
		mTakePicture = (Button) this.findViewById(R.id.take_picture);
		mTakePicture.setOnClickListener(this);
		mUploadRecord = (Button) this.findViewById(R.id.upload_record);
		mUploadRecord.setOnClickListener(this);

		mAdapter = new SignInRecordAdapter(this);
		mListView.setAdapter(mAdapter);

		if (mSponsorInfo != null) {
			mSignInDetailTime.setText(mSponsorInfo.getTime());
			mSignInDetailAddress.setText(mSponsorInfo.getAddress());
			mSignInDetailContent.setText(mSponsorInfo.getContent());
		} else {
			mAdapter.setTakePictureListener(this);
			mSignInDetailTime.setText("--");
			mSignInDetailAddress.setText("--");
			mSignInDetailContent.setText("--");
			mOthersDetail.setVisibility(View.GONE);
			mSigninNote.setVisibility(View.VISIBLE);
			mSoundRecord.setVisibility(View.GONE);
			mTakePicture.setVisibility(View.GONE);
		}
	}

	private void setAllButtonEnabled(boolean enable) {
		mBeginSignIn.setEnabled(enable);
		mCallSignIn.setEnabled(enable);
		mEndSignIn.setEnabled(enable);
		mCheckRecord.setEnabled(enable);
		mTakePicture.setEnabled(enable);
		mUploadRecord.setEnabled(enable);
	}

	/**
	 * 上传录音和照片
	 */
	private void uploadImageOrAudio() {
//		mUploadDialog.setMessage("请稍等，资源正在上传中...");
//		mUploadDialog.show();
		UploadToTempSyncLogic logic = new UploadToTempSyncLogic(
				mAppContext, mUploadHandler, true, mRegisterType);
		logic.execute();
	}

	/**
	 * 上传签到记录和各种资源
	 */
	private void upload(int index) {
		// 上传签到记录
		SignInUploadLogic logic = new SignInUploadLogic(mAppContext,
				mUploadHandler, mRegisterType);
		logic.execute();
		
	}

	private Handler mUploadHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			//TODO 更新视图
			mHandler.sendEmptyMessage(MSG_LOAD_DATA);

			switch (msg.what) {
				case UIHelper.SIGNIN_UPLOAD_SUCCESS:
					// 上传成功
					//TODO 上传资源
					uploadImageOrAudio();
					break;
				case UIHelper.SIGNIN_UPLOAD_ERROR:
				case UIHelper.SIGNIN_UPLOAD_FAIL:
					// 上传签到失败 --不区别失败的种类
					// upload(mUploadIndex);
					uploadImageOrAudio();
					break;
				case UIHelper.FILE_UPLOAD_SUCCESS:
//				mUploadDialog.cancel();
					if (mProgressDialog != null) {
						mProgressDialog.dismiss();
					}
					Toast.makeText(mAppContext, "记录上传成功！", Toast.LENGTH_SHORT)
							.show();
					break;
				case UIHelper.FILE_UPLOAD_FAIL:
//				mUploadDialog.cancel();
					if (mProgressDialog != null) {
						mProgressDialog.dismiss();
					}
					Toast.makeText(mAppContext, "记录上传失败！", Toast.LENGTH_SHORT)
							.show();
					break;
				default:
					break;
			}
		}

	};

	/**
	 * 执行指纹验证
	 */
	private void validateFingerPrint() {
		if (DEBUG) {
			// 调试--模拟指纹签到
			List<FingerPrintBean> list = mKjdb.findAllByWhere(
					FingerPrintBean.class, "type=1");
			if (list == null || list.isEmpty()) {
				FingerPrintBean bean = new FingerPrintBean();
				bean.setUserName("李峰");
				bean.setUserId("2000001480");
				bean.setType(1);
				addSignInRecord(bean);
			} else {
				Random radom = new Random();
				int i = radom.nextInt(list.size());
				FingerPrintBean bean = list.get(i);
				addSignInRecord(bean);
			}
		} else {
			mProgressDialog.setMessage("请按指纹...");
			mProgressDialog.show();
			mLogic.execute();
			mProgressDialog.setMatchLogic(mLogic);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.bengin_signin:
				// 开始签到
				mFlag = DataConfig.SIGN_FLAG_BEGIN;
				validateFingerPrint();
				break;
			case R.id.call_signin:
				// 点名签到
				mFlag = DataConfig.SIGN_FLAG_CALL;
				validateFingerPrint();
				break;
			case R.id.end_signin:
				// 结束签到
				mFlag = DataConfig.SIGN_FLAG_END;
				validateFingerPrint();
				break;
			case R.id.check_record:
				// 查看记录
				expandRecordList();
				break;
			case R.id.sound_record:
				// 录音
//			onClickRecordButton();
				SoundBean soundBean = new SoundBean();
				soundBean.serviceId = UploadToTempSyncLogic.SERVICE.SERVICE_COMMUNITY_IMPLODE;
				soundBean.source = mSponsorInfo;
				soundBean.registerType = mRegisterType;
				if ("1".equals(mRegisterType)){
					soundBean.modelName = "集中劳动";
				}else{
					soundBean.modelName = "集中教育";
				}
				Intent intent = new Intent(SignInDetailActivity.this,RecorderManagerActivity.class);
				intent.setAction(RecorderManagerActivity.INTENT_RECORDER_SIGN_DETAIL);
				intent.putExtra(RecorderManagerActivity.KEY_SOURCE,soundBean);
				intent.putExtra(RecorderManagerActivity.KEY_RECORD_COUNT,Integer.MAX_VALUE);
				startActivity(intent);
				break;
			case R.id.take_picture:
				// 拍照
				if (mPictureGridAdapter != null && mPictureGridAdapter.getCount() < 3){
					Intent takePicture = new Intent(this, ChoosePhotoActivity.class);
					takePicture.putExtra(ChoosePhotoActivity.CHOOSE_PHOTO_COUNT,
							mChooseImageCount);
					startActivityForResult(takePicture, SPONSOR_CHOOSE_PHOTO_REQUEST);
				}else{
					Toast.makeText(this,"最多选取三张照片",Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.upload_record:
				// 记录上传
				mProgressDialog.setIsOut(false);
				mProgressDialog.setMessage("请稍等,记录正在上传中...");
				mProgressDialog.show();
				upload(0);
				break;
			case R.id.header_back:
				if (mExpandShow) {
					shrinkRecordList();
				} else {
					finish();
				}
				break;
			case R.id.sign_note:
				try {
					mTakePictureItem = Integer.parseInt(v.getTag().toString());
					SignInRecordBean bean = mRecordList.get(mTakePictureItem);
					if (bean != null && !TextUtils.isEmpty(bean.getFilePath())){
						Intent mIntent = new Intent(SignInDetailActivity.this, PreviewPhotoActivity.class);
						ArrayList<String> paths = new ArrayList<String>();
						paths.add(bean.getFilePath());
						mIntent.putStringArrayListExtra("images", paths);
						mIntent.putExtra("position", 0);
						startActivity(mIntent);
						return;
					}
					// 拍照（个别劳动/个别教育-开始签到）
					Intent takePicture2 = new Intent(this,
							ChoosePhotoActivity.class);
					takePicture2
							.putExtra(ChoosePhotoActivity.CHOOSE_PHOTO_COUNT, 1);
					startActivityForResult(takePicture2,
							SIGNIN_CHOOSE_PHOTO_REQUEST);
				} catch (NumberFormatException e) {
					Log.e(TAG, "Parse " + v.getTag() + " to integer ERROR!");
				}
				break;
		}

	}

	/**
	 * 展开签到记录布局
	 */
	private void expandRecordList() {
		if (mExpandShow || mAnimationRunning)
			return;

		if (mOffsetY == 0) {
			mOffsetY = mContentDetail.getY() - mBaseDetail.getY();
		}

		float y = mBaseDetail.getY();
		ObjectAnimator baseAnimator = ObjectAnimator.ofFloat(mBaseDetail, "y",
				y - mOffsetY);
		baseAnimator.setDuration(ANIMATION_DURATION);

		y = mOthersDetail.getY();
		ObjectAnimator detailAnimator = ObjectAnimator.ofFloat(mOthersDetail,
				"y", y - mOffsetY);
		detailAnimator.setDuration(ANIMATION_DURATION);

		y = mContentDetail.getY();
		ObjectAnimator contentAnimator = ObjectAnimator.ofFloat(mContentDetail,
				"y", y - mOffsetY);
		contentAnimator.setDuration(ANIMATION_DURATION);

		y = mListView.getY();
		ObjectAnimator listAnimator = ObjectAnimator.ofFloat(mListView, "y", y
				- mOffsetY);
		listAnimator.setDuration(ANIMATION_DURATION);

		y = mControlDetail.getY();
		ObjectAnimator controlerAnimator = ObjectAnimator.ofFloat(
				mControlDetail, "y", y + mControlDetail.getMeasuredHeight());
		controlerAnimator.setDuration(ANIMATION_DURATION);

		AnimatorSet set = new AnimatorSet();
		set.playTogether(baseAnimator, detailAnimator, contentAnimator,
				listAnimator, controlerAnimator);
		set.addListener(new AnimatorListener() {

			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				mExpandShow = true;
				mAnimationRunning = false;
				mTitleView.setText(R.string.check_record);
			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationStart(Animator arg0) {
				mAnimationRunning = true;
				Log.i(TAG, "start to enpand RecordList!");
			}

		});
		set.start();
	}

	/**
	 * 收起签到详情状态
	 */
	private void shrinkRecordList() {
		if (!mExpandShow || mAnimationRunning)
			return;

		float y = mBaseDetail.getY();
		ObjectAnimator baseAnimator = ObjectAnimator.ofFloat(mBaseDetail, "y",
				y + mOffsetY);
		baseAnimator.setDuration(ANIMATION_DURATION);

		y = mOthersDetail.getY();
		ObjectAnimator detailAnimator = ObjectAnimator.ofFloat(mOthersDetail,
				"y", y + mOffsetY);
		detailAnimator.setDuration(ANIMATION_DURATION);

		y = mContentDetail.getY();
		ObjectAnimator contentAnimator = ObjectAnimator.ofFloat(mContentDetail,
				"y", y + mOffsetY);
		contentAnimator.setDuration(ANIMATION_DURATION);

		y = mListView.getY();
		ObjectAnimator listAnimator = ObjectAnimator.ofFloat(mListView, "y", y
				+ mOffsetY);
		listAnimator.setDuration(ANIMATION_DURATION);

		y = mControlDetail.getY();
		ObjectAnimator controlerAnimator = ObjectAnimator.ofFloat(
				mControlDetail, "y", y - mControlDetail.getMeasuredHeight());
		controlerAnimator.setDuration(ANIMATION_DURATION);

		AnimatorSet set = new AnimatorSet();
		set.playTogether(baseAnimator, detailAnimator, contentAnimator,
				listAnimator, controlerAnimator);
		set.addListener(new AnimatorListener() {

			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				mExpandShow = false;
				mAnimationRunning = false;
				mTitleView.setText(mTitleName);
//				mTitleView.setText(R.string.centralized_labor);
			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationStart(Animator arg0) {
				mAnimationRunning = true;
				Log.i(TAG, "start to shrink RecordList!");
			}

		});
		set.start();
	}

	private Runnable mFingerAgainRunnable = new Runnable() {

		@Override
		public void run() {
			validateFingerPrint();
		}

	};

	private Handler mFingerpringHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Message message = new Message();
			mSponsorType = 0;
			switch (msg.what) {
				case UIHelper.FINGERPRINT_UPCHAR_SUCCESS:
					break;
				case UIHelper.FINGERPRINT_UPCHAR_FAIL:// char--返回失败
				case UIHelper.FINGERPRINT_REGMODE_FAIL:// char--模板合成失败
				case UIHelper.FINGERPRINT_GENCHAR_FAIL:// char--生成特征值失败
				case UIHelper.FINGERPRINT_DELETECHAR_FAIL:// char--模板下载失败
				case UIHelper.FINGERPRINT_UPIMAGE_FAIL:// image--生成失败
				case UIHelper.FINGERPRINT_SEARCH_FAIL:
					message.what = 101;
					mMatchHandler.handleMessage(message);
					break;
				case UIHelper.FINGERPRINT_SEARCH_SUCCESS:
					// 匹配成功
					mSponsorType = msg.arg2;
					message.what = 100;
					message.arg1 = msg.arg1;
					mMatchHandler.handleMessage(message);
					break;
				case UIHelper.FINGERPRINT_AGAIN:// 再次按手指
					mProgressDialog.setIsOut(true);
					mProgressDialog.setMessage("请再次按指纹...");
					mProgressDialog.show();
				case UIHelper.FINGERPRINT_ONMATCH_SUCCESS:// image--图片合成成功

					break;

				default:
					break;
			}
		};

	};

	// 指纹验证结果回调
	private Handler mMatchHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (mProgressDialog != null) {
				mProgressDialog.dismiss();
			}
			if (mLogic != null) {
				mLogic.destory();
			}
			switch (msg.what) {
				case 100:
					// 匹配成功
					int pageId = msg.arg1;
					try {
						FingerPrintBean fingerPrintBean = mKjdb.findById(pageId,
								FingerPrintBean.class);
						addSignInRecord(fingerPrintBean);
					} catch (Exception e) {
					}
					new Handler().postDelayed(mFingerAgainRunnable,
							FINGER_AGAIN_TIME);
					break;
				case 101:
					//TODO 匹配失败 ，弹出图片签到的Dialog
					// 匹配失败
//					new Handler().postDelayed(mFingerAgainRunnable,
//							FINGER_AGAIN_TIME);
					takePhotoSign();
					break;

				default:
					break;
			}
		};
	};

	//进行拍照签到，选取矫正人员
	private void takePhotoSign() {
		// 拍照
		mRecordBean = null;
		new AlertDialog.Builder(this)
				.setTitle("拍照签到")
				.setMessage("没有找到匹配的指纹,是否进行拍照签到？")
				.setCancelable(true)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						chooseRecordInfo();
					}

				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}

				}).create().show();
	}

	// 选取矫正人员
	private void chooseRecordInfo() {
		Intent intent = new Intent(SignInDetailActivity.this, RecordActivity.class);
		intent.putExtra(RecordActivity.RECORD_STATE,
				RecordActivity.CHOOSE_RECORD_STATE);
		startActivityForResult(intent, RecordActivity.CHOOSE_RECORD_REQUEST_OK);
	}


	/**
	 * 添加签到信息
	 *
	 * @param fingerPrintBean
	 *            FingerPrintBean
	 */
	private void addSignInRecord(FingerPrintBean fingerPrintBean) {
		if (fingerPrintBean != null && fingerPrintBean.getType() == 1) {
//			if ("2".equals(mRegisterType) || "3".equals(mRegisterType)){
//				//在个别教育或者个别劳动的时候判断当前人员是否已经存在签到
//				if (mFlag == DataConfig.SIGN_FLAG_CALL || mFlag == DataConfig.SIGN_FLAG_END){
//					List<SignInRecordBean> beans = mKjdb.findAllByWhere(SignInRecordBean.class,"flag = '1' and name = " + fingerPrintBean.getUserName());
//					if (beans != null && beans.size() > 0){
//						//存在，可以继续
//					}else{
//						Toast.makeText(SignInDetailActivity.this, "请预先进行开始签到",
//								Toast.LENGTH_SHORT).show();
//						return;
//					}
//				}
//			}
			Toast.makeText(SignInDetailActivity.this, "签到成功",
					Toast.LENGTH_SHORT).show();
			SignInRecordBean bean = new SignInRecordBean();
			bean.setName(fingerPrintBean.getUserName());
			String time = DataUtils.converDatLongToString(
					System.currentTimeMillis(), DateFormat.DATE_YMD_HMS);
			bean.setTime(time);
			bean.setFlag(mFlag);
			bean.setState(1);
			bean.setType(mRegisterType);
			bean.setSponsorType(String.valueOf(mSponsorType));
			if (mSponsorInfo != null) {
				bean.setSponsorId(mSponsorInfo.getSponsorId());
			} else {
				bean.setSponsorId("");
			}
			//bean.setSponsorType(DataConfig.SIGN_TYPE_FINGERPRINT);
			bean.setElectronicsId(fingerPrintBean.getUserId());
			bean.setImie(mAppContext.getImei());
			bean.setRegistrationType(0);
			// 保存bean到数据库
			mKjdb.save(bean);
			DbModel dbModel = mKjdb
					.findDbModelBySQL("select max(id) as id from table_signInRecord order by id desc");
			int mMaxId = dbModel.getInt("id");
			Log.i(TAG, "-----》 id：" + mMaxId);
			bean.setId(mMaxId);
			mRecordList.add(bean);
			Collections.sort(mRecordList, mRecordComparator);
			mAdapter.setSignInRecordList(mRecordList);
			mAdapter.notifyDataSetChanged();
			mListView.setSelection(0);
		} else {
			Toast.makeText(SignInDetailActivity.this, "签到失败",
					Toast.LENGTH_SHORT).show();
		}
	}

	private SignInRecordBean saveSignInRecord() {
		if (mRecordBean != null) {
			Toast.makeText(SignInDetailActivity.this, "签到成功",
					Toast.LENGTH_SHORT).show();
			SignInRecordBean bean = new SignInRecordBean();
			bean.setName(mRecordBean.getName());
			String time = DataUtils.converDatLongToString(
					System.currentTimeMillis(), DateFormat.DATE_YMD_HMS);
			bean.setTime(time);
			bean.setFlag(mFlag);
			bean.setState(1);
			bean.setType(mRegisterType);
			bean.setSponsorType(String.valueOf(mSponsorType));
			bean.setRegistrationType(1);
			if (mSponsorInfo != null) {
				bean.setSponsorId(mSponsorInfo.getSponsorId());
			} else {
				bean.setSponsorId("");
			}
			//bean.setSponsorType(DataConfig.SIGN_TYPE_FINGERPRINT);
			bean.setElectronicsId(mRecordBean.getElectronicsId());
			bean.setImie(mAppContext.getImei());
			// 保存bean到数据库
			mKjdb.save(bean);
			DbModel dbModel = mKjdb
					.findDbModelBySQL("select max(id) as id from table_signInRecord order by id desc");
			int mMaxId = dbModel.getInt("id");
			Log.i(TAG, "-----》 id：" + mMaxId);
			bean.setId(mMaxId);
			mRecordList.add(bean);
			Collections.sort(mRecordList, mRecordComparator);
			mAdapter.setSignInRecordList(mRecordList);
			mAdapter.notifyDataSetChanged();
			mListView.setSelection(0);
			return bean;
		}
		Toast.makeText(SignInDetailActivity.this, "签到失败",
				Toast.LENGTH_SHORT).show();
		return null;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
									final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.i(TAG, "----onActivityResult--->requestCode:" + requestCode + ",resultCode:" + resultCode);
		if (requestCode == SPONSOR_CHOOSE_PHOTO_REQUEST
				&& resultCode == RESULT_OK) {
			//集中签到选取图片
			new Thread(new Runnable() {

				@Override
				public void run() {
					//TODO 待优化，此处还需要可以删除，修改图片
					mCacheCheckImages = data
							.getStringArrayListExtra("mCacheCheckImages");
					if (mCacheCheckImages != null
							&& !mCacheCheckImages.isEmpty()
							&& mCacheCheckImages.size() > 0) {
//						mPictureGridAdapter.clear();
						// 清除数据库中该活动的所有图片记录，否则记录将重复或超过上限
						StringBuilder where = new StringBuilder();
						where.append("sponsorType = '");
						where.append(mRegisterType);
						where.append("'");
						if (mSponsorInfo != null) {
							where.append(" and sponsorId = '");
							where.append(mSponsorInfo.getSponsorId());
							where.append("'");
						}
						where.append(" and fileType = 1");
//						mKjdb.deleteByWhere(ImageOrAudioBean.class,
//								where.toString());
						for (int i = 0; i < mCacheCheckImages.size(); i++) {
							String path = mCacheCheckImages.get(i);
							Log.i(TAG, "select picture path:" + path);
							boolean success = mPictureGridAdapter
									.addBitmap(path);
							if (success) {
								// 保存当前活动中（集中劳动或集中教育）该图片记录
								ImageOrAudioBean bean = DataHelper
										.getImageOrAudioBeanInfoByPath(
												mAppContext, path, 1);
								bean.setSponsorType(mRegisterType);
								if (mSponsorInfo != null) {
									bean.setSponsorId(mSponsorInfo
											.getSponsorId());
									bean.setServiceId(3);
								}else {
									bean.setServiceId(2);
								}
								bean.setImie(mAppContext.getImei());
								mKjdb.save(bean);
							}
						}
					}
				}
			}).run();
		}
		if (requestCode == SIGNIN_CHOOSE_PHOTO_REQUEST
				&& resultCode == RESULT_OK) {
			//个别签到，选取图片
			new Thread(new Runnable() {

				@Override
				public void run() {
					mCacheCheckImages = data
							.getStringArrayListExtra("mCacheCheckImages");
					if (mCacheCheckImages != null
							&& !mCacheCheckImages.isEmpty()
							&& mCacheCheckImages.size() > 0) {
						savePhoto();
					}
				}
			}).run();
		}
		//图片签到
		if (requestCode == DialogHelper.CHOOSE_PHOTO_RECORD_REQUEST_OK && resultCode == 1001) {
			if (data != null) {

				new Thread(new Runnable() {

					@Override
					public void run() {
						mCacheCheckImages = data
								.getStringArrayListExtra("mCacheCheckImages");
						if (mCacheCheckImages != null
								&& !mCacheCheckImages.isEmpty()
								&& mCacheCheckImages.size() > 0) {
							SignInRecordBean bean = saveSignInRecord();
							if (bean != null) {
								bean.setFilePath(mCacheCheckImages.get(0));
								mKjdb.update(bean);
								mAdapter.notifyDataSetChanged();
								//TODO 添加到资源数据库中
								ImageOrAudioBean b = DataHelper
										.getImageOrAudioBeanInfoByPath(
												mAppContext, mCacheCheckImages.get(0), 1);
								b.setParentId(String.valueOf(bean.getId()));
								b.setSponsorType(mRegisterType);
								if (mRegisterType.equals(DataConfig.REGISTER_TYPE_2)
										|| mRegisterType.equals(DataConfig.REGISTER_TYPE_3)) {
									b.setServiceId(2);
								} else {
									b.setServiceId(3);
								}
								b.setSponsorId(bean.getEduComld());
								b.setImie(mAppContext.getImei());
								b.setRegistrationType(2);
								mKjdb.save(b);
								//TODO 更新视图
								mHandler.sendEmptyMessage(MSG_LOAD_DATA);
								mListView.setSelection(0);
								mAdapter.notifyDataSetChanged();
							}
							mRecordBean = null;
						}
					}
				}).run();
			}
		}

		if (requestCode == RecordActivity.CHOOSE_RECORD_REQUEST_OK && resultCode == Activity.RESULT_OK) {
			if (data != null) {
				mRecordBean = (RecordBean) data
						.getSerializableExtra("RecordBean");
				if (mRecordBean != null
						&& !StringUtils.isEmpty(mRecordBean.getElectronicsId())) {
					//TODO 打开拍照信息
					takePhoto(DialogHelper.CHOOSE_PHOTO_RECORD_REQUEST_OK);
				} else {
					Toast.makeText(this, "选择失败，请重新选择矫正人员信息",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	private void savePhoto(){
		String filePath = mCacheCheckImages.get(0);
		Log.i(TAG, "----onActivityResult--->filePath:"
				+ filePath + ",mTakePictureItem："
				+ mTakePictureItem);
		if (mTakePictureItem >= 0 && mRecordList != null && !StringUtils.isEmpty(filePath)) {
			SignInRecordBean bean = mRecordList
					.get(mTakePictureItem);
			if (bean != null) {
				bean.setFilePath(filePath);
				mKjdb.update(bean);
				mAdapter.notifyDataSetChanged();
				//TODO 添加到资源数据库中
				ImageOrAudioBean b = DataHelper
						.getImageOrAudioBeanInfoByPath(
								mAppContext, filePath, 1);
				b.setParentId(String.valueOf(bean.getId()));
				b.setSponsorType(mRegisterType);
				if (mRegisterType.equals(DataConfig.REGISTER_TYPE_2)
						|| mRegisterType.equals(DataConfig.REGISTER_TYPE_3)) {
					b.setServiceId(2);
				}else {
					b.setServiceId(3);
				}
				b.setSponsorId(bean.getEduComld());
				b.setImie(mAppContext.getImei());
				mKjdb.save(b);
				//TODO 更新视图
				mHandler.sendEmptyMessage(MSG_LOAD_DATA);
				mListView.setSelection(0);
				mAdapter.notifyDataSetChanged();
			}
		}
	}

	private void takePhoto(final int requestId) {
		// 拍照
		new AlertDialog.Builder(this)
				.setTitle("拍照")
				.setMessage("是否进行拍照？")
				.setCancelable(true)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent takePicture = new Intent(
								SignInDetailActivity.this,
								ChoosePhotoActivity.class);
						takePicture.putExtra(
								ChoosePhotoActivity.CHOOSE_PHOTO_COUNT,
								1);
						startActivityForResult(takePicture,requestId);
					}

				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}

				}).create().show();
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mLogic != null) {
			mLogic.destory();
		}
		MtRfid.getInstance().RfidClose(); // Close
//		SerialPortManager.getInstance().closeSerialPort();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			if (mExpandShow) {
				shrinkRecordList();
				return true;
			} else {
				// FingerprintManager.getInstance().closeSerialPort();
				if (mLogic != null) {
					mLogic.destory();
				}
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
